package com.example.service;

import com.example.common.Result;
import com.example.entity.Article;
import com.example.entity.User;
import com.example.entity.UserActions;
import com.example.mapper.ArticleMapper;
import com.example.mapper.UserActionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private UserActionMapper userActionMapper;
    public Result addArticle(Article article) {
        Date date = new Date();
        article.setArticleTime(date);
        if(!articleMapper.addArticle(article)){
            return Result.error("新建失败");
        }
        return Result.success();
    }

    public Result listByUser(User user) {
        List<Article> result = new ArrayList<>();
        List<Article> articleList = articleMapper.listByUser();
        List<UserActions> userActionsList = userActionMapper.getArticleByUser(user);
        Map<Integer,List<UserActions>> userActionsMap = userActionsList.stream().collect(Collectors.groupingBy(x -> x.getResourceId()));
        for (Article article : articleList){
            if(userActionsMap.get(article.getArticleId())!=null){
                article.setIfLike(1);
            }else {
                article.setIfLike(0);
            }
            result.add(article);
        }
        return Result.success(result);
    }

    public Result searchArticle(Article article) {
        return Result.success(articleMapper.searchByTitle(article.getArticleTitle()));
    }

    public Result getArticle(Article article) {
        Article result = articleMapper.getArtileById(article.getArticleId());
        Integer num = result.getArticleCount() + 1;
        result.setArticleCount(num);
        articleMapper.update(result);
        return Result.success(result);
    }

    public Result likeArticle(UserActions userActions) {
        Article result = articleMapper.getArtileById(userActions.getResourceId());
        Integer num = result.getArticleLike() + 1;
        result.setArticleLike(num);
        articleMapper.update(result);

        Date date = new Date();
        userActions.setActionTime(date);
        userActionMapper.add(userActions);
        return Result.success(result);
    }

    public Result unLikeArticle(UserActions userActions) {
        Article result = articleMapper.getArtileById(userActions.getResourceId());
        Integer num = result.getArticleLike() - 1;
        result.setArticleLike(num);
        articleMapper.update(result);

        userActionMapper.delete(userActions);
        return Result.success(result);
    }

    public Result update(Article article) {
        if(articleMapper.update(article)){
            return Result.success("更新成功");
        }
        return Result.success("更新失败");
    }

    public Result listAll() {
        List<Article> articleList = articleMapper.listAll();
        return Result.success(articleList);
    }

    public Result delete(Article article) {
        if(!articleMapper.delete(article)){
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }
}
